﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using OpenPhotoAlbum.BusinessLogics;
using System.Web.Security;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum
{
    public partial class Login : AlbumPage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

			this.Title = StringUtility.AddAppPrefix(Resources.Strings.Title_Login);

            tabPanels.TabPanelItems.Add(new Controls.TabPanelItem()
            {
                Name = Resources.Strings.Login_Login,
                PanelClientId = pnlLogin.ClientID,
                Visible = true
            });
            tabPanels.TabPanelItems.Add(new Controls.TabPanelItem()
            {
                Name = Resources.Strings.Login_ForgotPassword,
                PanelClientId = pnlRecovery.ClientID,
                Visible = false
            });
			if (!string.IsNullOrEmpty(Request.QueryString[UrlFormats.PARAMETER_NAME_VISIBLE_INDEX]))
			{
				tabPanels.SetVisibleIndex(UrlContext.VisibleIndex);
			}

            ltrUserName.Text = StringUtility.AddColon(Resources.Strings.UserName);
			ltrPassword.Text = StringUtility.AddColon(Resources.Strings.Password);
            btnLogin.Text = Resources.Strings.Login_Login;
            cbRememberMe.Text = Resources.Strings.Login_RememberMe;

			msgError.MessageType = OpenPhotoAlbum.Controls.MessageTypes.FailMessage;

			ltrUserName2.Text = StringUtility.AddColon(Resources.Strings.UserName);
            ltrEmail.Text = StringUtility.AddColon(Resources.Strings.Email);
            btnSend.Text = Resources.Strings.Send;
            btnSend.Click += new EventHandler(btnSend_Click);

			if (!string.IsNullOrEmpty(UrlContext.UserName))
			{
				ActionManager.Instance.AddLoginFailure(UrlContext.UserName);
				msgError.Text = Resources.Strings.Login_WrongInput;
				tbUserName.Text = UrlContext.UserName;
			}

			hlJoin.Text = Resources.Strings.CreateYourAccount;
			hlJoin.NavigateUrl = Links.Join;
        }

		protected override void AppendSiteMapNodes(OpenPhotoAlbum.Controls.AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			provider.AppendSiteMapNode(Resources.Strings.SC_Home, Links.Default);
			provider.AppendSiteMapNode(Resources.Strings.Login_Login, RawUrl);
		}

        protected void btnSend_Click(object sender, EventArgs e)
        {
			tabPanels.SetVisibleIndex(1);
			message.MessageType = MessageTypes.FailMessage;

			string userName = tbUserName2.Text.Trim();
			string userEmail = tbEmail.Text.Trim();

			if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userEmail))
			{ message.Text = Resources.Strings.Login_IncompleteInput; return; }

            DbAccess.User user = BusinessLogics.UserManager.Instance.GetUserByEmail(userName, userEmail);
			if (user == null) { message.Text = Resources.Strings.Login_UserNameOrEmailIncorrect; return; }

            bool success = BusinessLogics.RecoveringRequestManager.Instance.AddNewRequest(user);
			if (!success) { message.Text = Resources.Strings.SystemError; return; }

			message.MessageType = MessageTypes.SuccessMessage;
			message.Text = Resources.Strings.Login_EmailSent;

			userName = user.UserName;
			userEmail = user.UserEmail;

			BusinessLogics.ActionManager.Instance.AddPasswordRecoveryEmailSent(userName, userEmail);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = tbUserName.Text.Trim();
			string password = tbPassword.Text.Trim();

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                msgError.Text = Resources.Strings.Login_IncompleteInput;
                return;
            }

            bool rememberMe = cbRememberMe.Checked;

            if (UserManager.Instance.ValidateUser(userName, password))
            {
				userName = BusinessLogics.UserManager.Instance.GetUser(userName).UserName;
				ActionManager.Instance.AddLoginSuccess(userName);
                FormsAuthentication.SetAuthCookie(userName, rememberMe);
                Response.Redirect(Links.MyHome);
            }
            else
            {
				ActionManager.Instance.AddLoginFailure(userName);
                msgError.Text = Resources.Strings.Login_WrongInput;
            }
        }
    }
}
